/**
 * 
 */
package UsedCar;

import java.util.Enumeration;
import java.util.Hashtable;

/**
 * @author fychao
 * 
 */
public class Impression {

	/**
	 * @param IMP_POS
	 *            正向印象 10
	 * @param IMP_NEG
	 *            負向印象 -10
	 * @param IMP_READ
	 *            讀過的印象 1
	 * @param MARGIN_RATIO
	 *            3
	 */
	public static final int IMP_POS = 10, IMP_NEG = -10, IMP_SHARE = 1,
			MARGIN_RATIO = 3;

	public static void main(String[] args) {
		Impression obj = new Impression();
		obj.addImp("ABC", 2);
		obj.addImp("ABC", 3);
		obj.addImp("DEF", 6);
		obj.addImp("DEF", -7);
		obj.listImp();

	}

	public Hashtable<String, Integer> data = new Hashtable<String, Integer>();
	public String max_key = "";

	public int max_val = 0;

	/**
	 * 加入一個印像值
	 * 
	 * @param target
	 *            目標字串
	 * @param val
	 *            值
	 */
	public void addImp(String target, int val) {
		int imp_v = 0;
		if (data.containsKey(target)) {
			imp_v = data.get(target);
		}
		if ((imp_v + val) < Impression.MARGIN_RATIO * Impression.IMP_NEG
				&& (imp_v + val) > Impression.MARGIN_RATIO * Impression.IMP_POS) {
			imp_v += val;
		}
		this.data.put(target, imp_v);

		this.setMax(target, val);
	}

	public double getInflu4Formula(String seller) {
		if (this.isReg(seller)) {
			int impv = this.data.get(seller);
			if (impv > 0) {
				return (float) ((impv > 0) ? 1 : -1)
						* (1 + (impv / Impression.IMP_POS));
			} else {
				return 0.0f;
			}
		} else {
			return 0.0f;
		}
	}

	/**
	 * 取得所有的鍵值 while (e.hasMoreElements()) System.out.println(e.nextElement());
	 * 
	 * @return 所有鍵值
	 */
	public Enumeration<String> getAllKey() {
		return this.data.keys();
	}

	/**
	 * 取得最大印像鍵
	 * 
	 * @return 最大的印像鍵
	 */
	public String getMaxKey() {
		return this.max_key;
	}

	/**
	 * 取得最大印像值
	 * 
	 * @return 最大的值
	 */
	public int getMaxVal() {
		return this.max_val;
	}

	/**
	 * 取得賣家的影響值
	 * 
	 * @param sellerString
	 * @return
	 */
	public int getVal(String sellerStr) {
		if (this.isReg(sellerStr)) {
			return this.data.get(sellerStr);
		} else {
			return 0;
		}
	}

	/**
	 * 是否有資料
	 * 
	 * @return boolean
	 */
	public boolean isNull() {
		return (this.data.size() == 0);
	}

	/**
	 * @param key
	 *            取得特定鍵值
	 * @return 該鍵的數值
	 */
	public boolean isReg(String key) {
		return this.data.containsKey(key);
	}

	public void listImp() {
		System.out.println(" ================= ");
		Enumeration<String> keys = this.data.keys();
		while (keys.hasMoreElements()) {
			String key = keys.nextElement();
			int val = this.data.get(key);
			System.out.println(key + " = " + val);
		}

		System.out.println("( Max )\n" + this.max_key + " = " + this.max_val);
	}

	/**
	 * 尋找最大的值
	 * 
	 * @return 最大的鍵
	 */
	protected String searchMaxKey() {
		String max = "";
		int max_val = -99;
		Enumeration<String> keys = this.data.keys();
		while (keys.hasMoreElements()) {
			String key = keys.nextElement();
			int val = this.data.get(key);
			if (max_val < val) {
				max_val = val;
				max = key;
			}
		}

		return max;
	}

	/**
	 * 自動設定最大印像數值
	 * 
	 * @param tar
	 *            最大的鍵
	 * @param val
	 *            最大的值
	 */
	public void setMax(String tar, int val) {

		if (this.max_key.length() <= 0) {
			this.max_key = tar;
			this.max_val = val;
		} else {
			if (this.max_val < this.data.get(tar)) {
				this.max_key = tar;
				this.max_val = this.data.get(tar);
			} else {
				this.max_key = this.searchMaxKey();
				this.max_val = this.data.get(this.max_key);
			}
		}

	}

}
